/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package Clases;

import java.util.ArrayList;

/**
 *
 * @author hC4stillo
 */
public class ModeloLogaritmico {
    public Comun comun;
    public ArrayList<Object> x;
    public ArrayList<Object> y;
    public String r;
    public String rCuadrada;
    public String sexy;
    public ModeloLogaritmico(ArrayList<Object> x,ArrayList<Object> y)
    {
        this.x=x;
        this.y=y;
        this.comun = new Comun(x,y);
    }
    public ArrayList<Object> getLnX()
    {
        ArrayList<Object> lnX=new ArrayList();
        for(int i=0;i<this.x.size();i++)
            lnX.add(Math.log(Double.parseDouble(this.x.get(i).toString())));
        return lnX;
    }
    
    public ArrayList<Object> getLnXC()
    {
        ArrayList<Object> lnX = this.getLnX();
        ArrayList<Object> lnXC = new ArrayList();
        for(int i=0;i<this.y.size();i++)
            lnXC.add(Math.pow(Double.parseDouble(lnX.get(i).toString()), 2));
        return lnXC;
    }
    public ArrayList<Object> getLnXY()
    {
        ArrayList<Object> lnX = this.getLnX();
        ArrayList<Object> lnXY = new ArrayList();
        for(int i=0;i<this.y.size();i++)
            lnXY.add(Double.parseDouble(lnX.get(i).toString())*Double.parseDouble(this.y.get(i).toString()));
        return lnXY;
    }
    public double sumatoria(ArrayList<Object> arreglo)
    {
        double total=0;
        for(int i = 0;i<this.x.size();i++)
            total = total + Double.parseDouble(arreglo.get(i).toString());
        //System.out.println("suma = "+total);
        return total;
    }
    public double promedio(double suma)
    {
        //System.out.println(".."+(suma/this.x.size()));
        return (suma/this.x.size());
    }
    public double getB()
    {
        double b=0;
        double promLnXY = this.promedio(this.sumatoria(this.getLnXY()));
        double promLnX = this.promedio(this.sumatoria(this.getLnX()));
        double promY = this.promedio(this.sumatoria(this.y));
        double promXC = this.promedio(this.sumatoria(this.getLnXC()));
        double promXalC = Math.pow(promLnX, 2);        
        b = (promLnXY - (promLnX * promY))/(promXC - promXalC);
        System.out.println("("+promLnXY+" - ("+promLnX+" * "+promY+"))/("+promXC+" - "+promXalC+")");
        System.out.println("b= "+b);
        return b;
    }
    public double getA()
    {
        double a = 0;
        double b = this.getB();
        double promY = this.promedio(this.sumatoria(this.y));
        double promLnX = this.promedio(this.sumatoria(this.getLnX()));
        a = promY - (b*promLnX);
        System.out.println("a= "+a);
        return a;
    }
    //blnX+a
    public ArrayList<Object> getYCalculada()
    {
        ArrayList<Object> yCalculada = new ArrayList();
        ArrayList<Object> lnX = this.getLnX();
        double b = getB();
        double a = getA();
        for(int i = 0;i<this.x.size();i++)
        {
            Double temp = b * Double.parseDouble(lnX.get(i).toString());
            temp = temp + a;
            System.out.println("y'="+temp);
            yCalculada.add(temp);
        }
        return yCalculada;
    }

    public void setR()
    {
        double promLnXY = this.promedio(this.sumatoria(this.getLnXY()));
        double promLnX = this.promedio(this.sumatoria(this.getLnX()));
        double promY = this.promedio(this.sumatoria(this.y));
        

        double promXC = this.promedio(this.sumatoria(this.getLnXC()));
        double promXalC = Math.pow(promLnX, 2);
        double promYC = this.promedio(this.sumatoria(this.comun.getYCuadrado()));
        double promYalC = Math.pow(promY, 2);
        
        double part1 = promLnXY - (promLnX * promY);
        double part2 = (promXC - promXalC) * (promYC - promYalC);
        part2 = Math.sqrt(part2);
        part1 = part1/part2;
        

        System.out.println("("+promLnXY+" - ("+promLnX+" * "+promY+")/raiz(("+promXC+" - "+promXalC+") * ("+promYC+" - "+promYalC+"))");
        System.out.println("");
        
        part1 = this.comun.reducirDecimales((float)part1);
        
        this.r = part1+"";
        this.rCuadrada = Math.pow(part1*100, 2)+"%";

    }

    public void SetSexy()
    {
        ArrayList<Object> varNoExp = new ArrayList();
        ArrayList<Object> yCalc = this.getYCalculada();
        for(int i = 0; i < this.y.size(); i++)
        {
            float temp = Float.parseFloat(this.y.get(i).toString()) - Float.parseFloat(yCalc.get(i).toString());
            temp = (float) Math.pow(temp, 2);

            varNoExp.add(temp);
        }
        double sxy = this.sumatoria(varNoExp);
        sxy = Math.sqrt(sxy/this.x.size());
        sxy = this.comun.reducirDecimales((float)sxy);
        this.sexy = sxy+"";
    }
    public void resolver()
    {
        ModeloLineal ml = new ModeloLineal(this.x,this.y);
        ml.calcularR(this.getYCalculada());
        ml.calcularSexy(this.getYCalculada());
        this.r = ml.r;
        this.rCuadrada = ml.rCuadrada;
        this.sexy = ml.sexy;

    }

}
